package com.iotboss.system.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.iotboss.common.constant.MessageConstant;
import com.iotboss.common.constant.WeChatConstant;
import com.iotboss.common.core.domain.entity.SysUser;
import com.iotboss.common.exception.LoginFailedException;
import com.iotboss.common.mapper.mysql.UserMapper;
import com.iotboss.common.pojo.dto.UserLoginDTO;
import com.iotboss.common.properties.WeChatProperties;
import com.iotboss.common.utils.WechatLoginUtils;
import com.iotboss.common.utils.http.HttpClientUtil;
import com.iotboss.system.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.User;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class UserServiceImpl implements UserService {
    @Autowired
    UserMapper userMapper;

    @Autowired
    WechatLoginUtils wechatLoginUtils;
    /**
     * 小程序用户登录
     * @return
     */
    @Override
    public SysUser login(UserLoginDTO userLoginDTO) throws IOException {
        //1.获取授权码
        String authCode = userLoginDTO.getAuthCode();
        String code = userLoginDTO.getCode();
        //2.根据授权码去微信后台获取openid(唯一)
        String openId = getOpenId(authCode);
        String phoneNumber = wechatLoginUtils.getPhoneNumber(code);
        if (openId == null || phoneNumber==null){
            //1.授权码是伪造的
            //2.授权码过期
            //3.授权码被使用过
            throw new LoginFailedException(MessageConstant.LOGIN_FAILED);
        }

        //3.根据openid查询
        SysUser user = userMapper.findUserByOpenId(openId);
        if (user==null){
            //没有用户,是新用户,注册
            SysUser.builder()
                    .phonenumber(phoneNumber)
                            .openId(openId)
                                    .build();
            user.setCreateTime(new Date());
            userMapper.save(user);
        }
        return user;
    }

    @Autowired
    WeChatProperties weChatProperties;

    /**
     * 根据授权码去微信获取对应的openid
     * @param authCode
     * @return
     */

    public String getOpenId(String authCode){
        Map<String, String> param = new HashMap<>();
        param.put(WeChatConstant.APP_ID,weChatProperties.getAppid());
        param.put(WeChatConstant.APP_SECRET,weChatProperties.getSecret());
        param.put(WeChatConstant.APP_JS_CODE,authCode);
        param.put(WeChatConstant.APP_GRANT_TYPE,WeChatConstant.AUTHORIZATION_CODE);
        String result = HttpClientUtil.doGet(WeChatConstant.WX_LOGIN_URL, param);
        Map<String,String> map = (Map) JSON.parse(result);
        return map.get(WeChatConstant.WX_OPEN_ID);
    }


}
